<?php
namespace app\admin\model;
use think\Model;
use think\Db;


class GoodscatsModel extends Model{
    public function goodsCatsList(){
        $data['is_del'] =0;
        $rows = Db::table('goods_type')->where($data)->select();
        $rows = $this->showTreetwo($rows);
        return  $rows;
    }
    //无限极分类
    public function showTreeone($arr, $html = "&nbsp;&nbsp;&nbsp;&nbsp;", $pid = 0, $level = 0){
        static $newArr = array();
        foreach($arr as $key => $v){
            if($v['parentId'] == $pid) {
                $v['level'] = $level + 1;
                $html2 = ($level == 0) ? '' : '|--';//����Ŀ¼|--
                $v['html'] = str_repeat($html, $level) . $html2;
                $v['catName'] = $v['html'] . $v['catName'];
                $newArr[] = $v;

                $this->showTreeone($arr, $html , $v['catId'], $v['level']);
            }
        }
        return $newArr;
    }
    //无限极分类2
    public function showTreetwo($arr, $html = "&nbsp;&nbsp;&nbsp;&nbsp;", $pid = 0, $level = 0){
        $newArr = array();
        foreach($arr as $key => $v){
            if($v['parent_id'] == $pid) {
                $v['level'] = $level + 1;
                $v['html'] = str_repeat($html, $level) ;
                $v['data'] = $this->showTreetwo($arr, $html , $v['id'], $v['level']);
                $newArr[] = $v;
            }
        }
        return $newArr;
    }

    public function delGoodsCats($id){
        $chickid['is_del'] =0;
        $res = Db::table('goods_type')->where($chickid)->select();
        //找到该条件的所有id
        foreach( $res  as  $v  ){
            if( $v['parent_id'] == $id  ){
                $data[]=$v['id'];
                $a=$v['id'];
                foreach( $res as  $b  ){
                    if( $b['parent_id'] == $a  ){
                        $data[]=$b['id'];
                    }
                }
            }
        }
        $data[] = $id;
        //循环逻辑删除
        foreach( $data   as  $zz   ){
            $xx['id'] =$zz;
            $mm['is_del'] =1;
            $del =Db::table('goods_type')->where($xx)->update($mm);
            if($del<1){
                return -1;
            }
        }
        return   $del;
    }
    public function doAddFirstCats($data){
        $rows = Db::table('goods_type')->insert($data);
        return  $rows;
    }
    public function doAddChildCart($data){
        $rows = Db::table('goods_type')->insert($data);
        return  $rows;
    }
    public function updateCat($data){
        $rows = Db::table('goods_type')->where($data)->find();
        return  $rows;
    }
    public function doUpdateCat($data,$chickid){
        $rows = Db::table('goods_type')->where($chickid)->update($data);
        return  $rows;
    }
    public function goodsInfoImagesList(){
        $data['is_del'] =0;
        $rows = Db::table('goods')
            ->alias('a')
            ->join('goods_info b','b.goods_id=a.id')
            ->where('a.is_del=0 and b.is_del=0')
            ->order('b.id desc')
            ->paginate(10);
        return  $rows;
    }
    public function getFirstCat(){
        $data['parent_id']=0;
        $data['is_del']=0;
        $rows = Db::table('goods_type')->where($data)->select();
        return   $rows;
    }
    public function getFirstCatId($catId){
        $data['parent_id']=$catId;
        $data['is_del']=0;
        $rows = Db::table('goods_type')->where($data)->select();
        return   $rows;
    }
    public function getSecondCatId($catId){
        $data['goods_cat_id2']=$catId;
        $data['is_del']=0;
        $rows = Db::table('goods')->where($data)->select();
        return   $rows;
    }
    public function uploadAllGoodsImages($data){
        $rows = Db::table('goods_info')->insert($data);
        return   $rows;
    }
    public function deleteGoodsInfo($id){
        $data['id']=$id;
        $update['is_del']=1;
        $rows = Db::table('goods_info')->where($data)->update($update);
        return   $rows;
    }
    public function goodsAppraises(){
        $rows = Db::table('goods')
            ->alias('a')
            ->join('goods_appraises b','b.goods_id=a.id')
            ->where('a.is_del=0 and b.is_del=0')
            ->order('b.id desc')
            ->paginate(10);
        return  $rows;
    }
    public function deleteGoodsAppraises($id){
        $data['id']=$id;
        $update['is_del']=1;
        $rows = Db::table('goods_appraises')->where($data)->update($update);
        return   $rows;
    }



}